﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices.WindowsRuntime;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Navigation;

using HappyDining.HPDNServiceReference;
using System.Diagnostics;

// The User Control item template is documented at http://go.microsoft.com/fwlink/?LinkId=234236

namespace HappyDining.MyUserControls
{
    public sealed partial class QuanLyMonControl : UserControl
    {
        public event EventHandler EditRequested;
        public event EventHandler CloseRequested;

        public QuanLyMonControl()
        {
            this.InitializeComponent();
            init();
        }

        private async void init() 
        {
            Service1Client client = new Service1Client();
            List<Category> categories = await client.GetAllCategoriesAsync();
            this.categoriesListView.ItemsSource = categories;

            Category c = categories[0];
            List<Item> items = await client.GetItemsByCategoryAsync(c);
            this.itemListView.ItemsSource = items;

            
        }

        private async void categoriesListView_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            Service1Client client = new Service1Client();
            Category c = (Category) this.categoriesListView.SelectedItem;
            List<Item> items = await client.GetItemsByCategoryAsync(c);
            this.itemListView.ItemsSource = items;
        }

        
        private void EditButton_Click(object sender, RoutedEventArgs e)
        {
            Button clickedButton = (sender as Button);
            string command = clickedButton.CommandParameter.ToString();
            Debug.WriteLine("Command: " + command);

            if (EditRequested != null)
            {
                this.EditRequested(sender, new ItemEditEventArgs(command));
            }
        }
    }

    public class ItemEditEventArgs : EventArgs
    {
        public int itemId;
        public ItemEditEventArgs(string id)
        {
            Int32.TryParse(id, out itemId);
        }
    }
}
